package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.FactoryMapper;
import com.clkj.xiuzheng.pojo.Company;
import com.clkj.xiuzheng.pojo.Factory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class FactoryServiceImpl implements FactoryService{
    @Autowired
    private FactoryMapper factoryMapper;

    @Override
    public void addFactory(Factory factory) {
        factoryMapper.insertSelective(factory);
    }

    @Override
    public void updateFactory(Integer factoryId,Integer status) {
        Factory factory = new Factory();
        factory.setFactoryId(factoryId);
        factory.setFactoryStatus(status);
        factoryMapper.updateByPrimaryKeySelective(factory);
    }

    @Override
    public void updateFactoryUp(Integer factoryId) {
        factoryMapper.updateFactoryUpById(factoryId);
    }

    @Override
    public PageInfo getAllFactory(Integer pageSize, Integer pageNum,Integer companyId) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",companyId);
        return new PageInfo(factoryMapper.selectByExample(example));
    }

    @Override
    public PageInfo getFactoryByName(Integer pageSize, Integer pageNum, String factoryName,Integer companyId) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Factory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",companyId).andCondition(" factory_name like '%"+factoryName+"%'");

        return new PageInfo(factoryMapper.selectByExample(example));
    }

}
